package com.bdqn.tour.servlet.comments;


import com.bdqn.tour.dao.HotelImplementationClass;
import com.bdqn.tour.dao.impl.ImplUersDao;
import com.bdqn.tour.entity.Comment;
import com.bdqn.tour.entity.Xingon;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@WebServlet("/index")

public class UerServlet extends HttpServlet {
    private static final HotelImplementationClass uerdao = new ImplUersDao();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        int a=3;
        // 检查请求是否为多部分表单数据3--
        if (!ServletFileUpload.isMultipartContent(req)) {
            resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "请求不是多部分表单数据");
            return;
        }

        final String UPLOAD_DIR = "uploads";
        String uploadPath = getServletContext().getRealPath("/") + File.separator + UPLOAD_DIR;
        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }

        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(factory);

        String uploadDirParam = null;
        String comments = null;
        String xi = null;
        String name = null;
        String filePath = null;


        try {
            List<FileItem> items = upload.parseRequest(req);
            for (FileItem item : items) {
                if (item.isFormField()) {
                    // 处理表单字段
                    String fieldName = item.getFieldName();
                    String fieldValue = item.getString("UTF-8");
                    switch (fieldName) {
                        case "up":
                            uploadDirParam = fieldValue;
                            break;
                        case "comments":
                            comments = fieldValue;
                            break;
                        case "hobby":
                            xi = fieldValue;
                            break;
                        case "uer":
                            name = fieldValue;
                            break;
                    }
                } else {
                    // 处理文件字段
                    String fileName = UUID.randomUUID().toString() + "_" + item.getName();
                    File uploadedFile = new File(uploadPath, fileName);
                    item.write(uploadedFile);

                    // 存储文件路径
                    filePath = UPLOAD_DIR + File.separator + fileName;
                }
            }

            // 创建 Comment 对象并设置属性
            Comment users = new Comment();
            users.setUsercomment(comments);
            users.setUsername(name);
            users.setXi(xi);
            users.setImg(filePath);
            //从数据库中拿数据总数
            int tou=uerdao.getTotalCommentsCount1();//获得总数居的总数
             users.setPagesize(tou);
             users.setPageNo(a);

            String xix=null;//通过判断给好评
            switch (users.getXi()) {
                case "1":
                    xix = "超差";
                    break;
                case "2":
                    xix = "差";
                    break;
                case "3":
                    xix = "一般";
                    break;
                case "4":
                    xix = "好";
                    break;
                case "5":
                    xix = "超棒";
                    break;
            }
            users.setXin( xix);

            // 添加用户评论到数据库
            uerdao.addUsers(users);

            List<Comment> commentsList = uerdao.getComments(1,3);
            if (commentsList != null) {
                // 处理数据
                Xingon Xin= uerdao.getXi();
                int Zong=uerdao.getTotalCommentsCount1();
                double Haopin=(double)Xin.getHaopin()/Zong*100;
                double Chapin=(double)Xin.getChapin()/Zong*100;
                double Zongpin=(double)Xin.getZongpin()/Zong*100;
                req.setAttribute("Zongpin", Zongpin+"%");
                req.setAttribute("Chapin", Chapin+"%");
                req.setAttribute("Haopin", Haopin+"%");
                System.out.println("Haopin:"+Haopin);
                System.out.println("Zongpin:"+Zongpin);
                System.out.println("Chapin:"+Chapin);

                req.setAttribute("commentsList", commentsList);
                req.getRequestDispatcher("/HotelReviews.jsp").forward(req, resp);
            } else {
                resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "评论数据不存在");
            }

        } catch (Exception e) {
            e.printStackTrace();
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "评论和图片上传失败: " + e.getMessage());
        }
    }
}